package apis

import (
	"xiaoercaidan/publicResources/models"
	"xiaoercaidan/baseApi/dao"
	"encoding/json"
	"xiaoercaidan/publicResources/utils"
	"xiaoercaidan/publicResources/controller"
	"github.com/goinggo/mapstructure"
)

type GoodsApi struct {
	controller.BaseController
}

func CheckParamsShopId(c *GoodsApi, shopId string) bool {

	if (len(shopId) <= 0) {
		var BaseResult = new(models.BaseResult)
		BaseResult.Code = 100
		BaseResult.Msg = "必须传入ShopId参数"
		c.Data["json"] = &BaseResult
		c.ServeJSON()
		return false
	}

	return true

}


// @Title AddCategory
// @Description 添加分类
// @Param   cate     patch string   true int64       "分类json"
// @Success 200 {object} models.Category
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /addCategory [get]
func (c *GoodsApi) AddCategory() {

	var BaseResult = new(models.BaseResult)

	var cateStr = c.GetString("cate")

	if ( len(cateStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"
	} else {

		var mapStr map[string]interface{}

		json.Unmarshal([]byte(cateStr), &mapStr)

		for k := range mapStr {
			if k == "Id" {
				mapStr[k] = nil
			}
		}

		var cate = new(models.Category)

		mapstructure.Decode(mapStr, cate);


		if err := dao.InserCategory(cate); (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = cate
		}

	}
	c.Data["json"] = BaseResult

	c.ServeJSON()

}




// @Title QueryCategory
// @Description 查询分类，父ID为0则查询所有分类
// @Param   parentId    patch int64  true  string       "父类分ID"
// @Param   shopId    patch int64  true  string       "店铺ID"
// @Success 200 {object} models.Category
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /queryCategory [get]
func (c *GoodsApi) QueryCategory() {

	var parentId = c.GetString("parentId")
	var shopId = c.GetString("shopId")

	CheckParamsShopId(c, shopId)

	var BaseResult = new(models.BaseResult)

	var params = make(map[string]string)

	params["shopId"] = shopId

	if (len(parentId) > 0) {
		params["parent_id"] = parentId
	}

	var shops = dao.QeryCategory(params)

	BaseResult.Data = shops

	c.Data["json"] = &BaseResult
	c.ServeJSON()

}



// @Title CategoryUpdate
// @Description 分類修改
// @Param   cate     patch string   true string       "分类json数据"
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /categoryUpdate [get]
func (this *GoodsApi) CategoryUpdate() {

	var BaseResult = new(models.BaseResult)
	var category = new(models.Category)

	var categoryJson = this.GetString("cate")

	if ( len(categoryJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"

	} else {

		json.Unmarshal([]byte(categoryJson), &category)

		if (category != nil && category.Id > 0) {
			err := dao.UpdateCategory(category)

			if (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = category
			}

		} else {
			BaseResult.Code = 100
			BaseResult.Msg = "没有查询到相关结果"

		}

	}

	this.Data["json"] = &BaseResult
	this.ServeJSON()

}







// @Title AddGoods
// @Description 新增商品
// @Param   goods     patch string   true string       "商品json"
// @Success 200 {object} models.Goods
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /addGoods [get]
func (c *GoodsApi) AddGoods() {

	var BaseResult = new(models.BaseResult)

	var goodsStr = c.GetString("goods")
	if (len(goodsStr) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数为空"
	} else {

		var mapStr map[string]interface{}
		var imageList []interface{}
		json.Unmarshal([]byte(goodsStr), &mapStr)
		imageList = mapStr["ImageList"].([]interface{})
		for k := range mapStr {
			if k == "Id" {
				mapStr[k] = nil
			}
		}
		var goods = new(models.Goods)
		mapstructure.Decode(mapStr, goods);
		var ImageList   []models.GoodsImage
		for _, value := range imageList {
			var image = new(models.GoodsImage)
			mapstructure.Decode(value, image);
			ImageList = append(ImageList, *image)
		}
		goods.ImageList = ImageList

		if err := dao.InserGoods(goods); (utils.CheckResult(BaseResult, err)) {
			BaseResult.Data = goods
		}

		for _, value := range ImageList {
			value.GoodId = goods.Id
			dao.InserImage(value)
		}

	}
	c.Data["json"] = BaseResult

	c.ServeJSON()

}


// @Title DelCategory
// @Description 删除分类
// @Param   ids     patch string   true int64       "要删除的分类ID列表，以“，”分割"
// @Success 200 delete success
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /delCategory [get]
func (c *GoodsApi) DelCategory() {

	var BaseResult = new(models.BaseResult)

	var ids = ""

	c.Ctx.Input.Bind(&ids, "ids")

	err := dao.DeleteCategory(ids)

	if (err != nil) {
		BaseResult.Code = 100
		BaseResult.Msg = err.Error()
	} else {
		BaseResult.Msg = "删除成功"
	}

	c.Data["json"] = &BaseResult
	c.ServeJSON()

}

// @Title DelGoods
// @Description 删除商品
// @Param   goods     patch string   true int64       "要删除的商品ID列表，以“，”分割"
// @Param   shopId int64 "店铺id"
// @Success 200 delete success
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /delGoods [get]
func (c *GoodsApi) DelGoods() {

	var BaseResult = new(models.BaseResult)

	var goods = c.GetString("goods")
	var shopId = c.GetString("shopId")

	if ( len(shopId) <= 0 || len(goods) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "店铺ID和商品ID不能为空"
	} else {

		err := dao.DeleteGoods(shopId, goods)

		if (err != nil) {
			BaseResult.Code = 100
			BaseResult.Msg = err.Error()
		} else {
			BaseResult.Msg = "删除成功"
		}

	}

	c.Data["json"] = &BaseResult
	c.ServeJSON()

}

// @Title QueryGoods
// @Description 查询商品
// @Param   name    patch int64  true  string       "商品关键字"
// @Param   categoryId    patch int64  true  string       "分类ID"
// @Param   shopId    patch int64  true  string       "门店ID"
// @Success 200 delete success
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /queryGoods [get]
func (c *GoodsApi) QueryGoods() {

	var name = c.GetString("name")
	var category_id = c.GetString("categoryId")
	var shop_id = c.GetString("shopId")

	CheckParamsShopId(c, shop_id)

	var BaseResult = new(models.BaseResult)

	var params = make(map[string]string)

	params["name"] = name

	if (len(category_id) > 0) {
		params["category_id"] = category_id
	}

	params["shop_id"] = shop_id

	var goods = dao.QeryGoods(params)

	BaseResult.Data = goods

	c.Data["json"] = &BaseResult
	c.ServeJSON()

}

// @Title QueryGoodsDetail
// @Description 查询商品详情
// @Param   id    patch int64  true  string       "id"
// @Success 200 delete success
// @Failure 400 Invalid params supplied
// @Failure 404 page not found
// @router /queryGoodsDetail [get]
func (c *GoodsApi) QueryGoodsDetail() {
	var goodsId = c.GetString("id")
	var BaseResult = new(models.BaseResult)
	if ( len(goodsId) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "ID不能为空"
	} else {
		goods := dao.QeryGoodsById(goodsId)
		image := dao.QeryImageByGoodsId(goods.Id)
		if(image != nil){
			goods.ImageList = *image
		}
		BaseResult.Data = goods
	}
	c.Data["json"] = &BaseResult
	c.ServeJSON()
}


// @Title GoodsUpdate
// @Description 商品修改
// @Param   goods     patch string   true string       "商品json数据"
// @Failure 400 Invalid params supplied
// @Failure 404 api not found
// @router /updateGoods [get]
func (this *GoodsApi) GoodsUpdate() {

	var BaseResult = new(models.BaseResult)
	var good = new(models.Goods)

	var goodsJson = this.GetString("goods")

	if ( len(goodsJson) <= 0 || len(goodsJson) <= 0) {
		BaseResult.Code = 100
		BaseResult.Msg = "参数不能为空"
	} else {

		json.Unmarshal([]byte(goodsJson), &good)

		if (good == nil || good.Id <= 0) {
			BaseResult.Code = 100
			BaseResult.Msg = "没有查询到结果"
		} else {
			err := dao.UpdateGoods(good)

			if (utils.CheckResult(BaseResult, err)) {
				BaseResult.Data = good
			}
		}

	}
	this.Data["json"] = &BaseResult
	this.ServeJSON()
}

